package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.*;
import cn.wolfcode.crm.mapper.ClientMapper;
import cn.wolfcode.crm.mapper.OfficialStudentMapper;
import cn.wolfcode.crm.mapper.StudentChangeClassMapper;
import cn.wolfcode.crm.mapper.StudentLossItemMapper;
import cn.wolfcode.crm.query.ChartQueryObj;
import cn.wolfcode.crm.query.OfficialStudentQueryObj;
import cn.wolfcode.crm.service.IOfficialStudentService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OfficialStudentServiceImpl implements IOfficialStudentService {

    @Autowired
    private OfficialStudentMapper officialStudentMapper;
    @Autowired
    private StudentChangeClassMapper studentChangeClassMapper;
    @Autowired
    private StudentLossItemMapper studentLossItemMapper;
    @Autowired
    private ClientMapper clientMapper;


    public void delete(Long id) {
        if(id!=null){
            officialStudentMapper.deleteByPrimaryKey(id);
        }
    }

    public void save(OfficialStudent entity) {
        if(entity!=null){
            //把客户从资源池移除
            clientMapper.updateInPool(entity.getId());
            //添加
            officialStudentMapper.insert(entity);
            //设置未交学费
            BigDecimal totalTui = new BigDecimal(entity.getTotalTuition().getName());
            BigDecimal hasDealTuition = entity.getHasDealTuition();
            BigDecimal divide = totalTui.subtract(hasDealTuition);
            officialStudentMapper.updateNoDealTuition(divide,entity.getId());

        }
    }

    public void update(OfficialStudent entity) {
        officialStudentMapper.updateByPrimaryKey(entity);
    }

    public OfficialStudent get(Long id) {
        return officialStudentMapper.selectByPrimaryKey(id);
    }

    public List<OfficialStudent> list() {
        return officialStudentMapper.selectAll();
    }

    public PageResult query(OfficialStudentQueryObj qo) {
        //查总数
        int total = officialStudentMapper.queryForCount(qo);
        if(total == 0){
            return new PageResult();
        }
        //查数据
        List<OfficialStudent> rows = officialStudentMapper.queryForList(qo);
        for (OfficialStudent row : rows) {
            String aa = row.getTotalTuition().getName();
            BigDecimal totalTui = new BigDecimal(aa);
            BigDecimal hasDealTuition = row.getHasDealTuition();
            BigDecimal divide = totalTui.subtract(hasDealTuition);
            row.setNoDealTuition(divide);
            if(new BigDecimal(0).equals(divide)){
                row.setDealTuitionState(1);
            }
        }
        return new PageResult(total, rows);
    }

    //班级转换保存
    public void saveChangeClass(OfficialStudent entity) {
        if(entity!=null){
            OfficialStudent officialStudent = officialStudentMapper.selectByPrimaryKey(entity.getId());
            officialStudent.setTargetClazz(entity.getTargetClazz());
            StudentChangeClass studentChangeClass = new StudentChangeClass();
            studentChangeClass.setAuditState(0);
            studentChangeClass.setBeforeClass(officialStudent.getClazz());
            studentChangeClass.setChangeClassTime(new Date());
            studentChangeClass.setHasTuition(officialStudent.getHasDealTuition());
            studentChangeClass.setNoTuition(officialStudent.getNoDealTuition());
            studentChangeClass.setMarketingPerson(officialStudent.getMarketingPerson());
            studentChangeClass.setName(officialStudent.getName());
            studentChangeClass.setTel(officialStudent.getTel());
            studentChangeClass.setNowClass(entity.getTargetClazz());
            studentChangeClass.setTotalTuition(officialStudent.getTotalTuition());
            studentChangeClass.setBeforeStuId(officialStudent.getId());
            studentChangeClassMapper.insert(studentChangeClass);
            officialStudentMapper.updateStudyStateById(entity.getId());
        }
    }

    public void quitClass(Long id) {
        if(id!=null){
            officialStudentMapper.quitClass(id);
        }
    }

    public void recoverClass(Long id) {
        if(id!=null){
            officialStudentMapper.recoverClass(id);
        }
    }

    //学员流失
    public void saveLossStu(StudentLossItem entity) {
        if(entity!=null){
            OfficialStudent officialStudent = officialStudentMapper.selectByPrimaryKey(entity.getId());
            entity.setQqNum(officialStudent.getQqNum());
            entity.setTel(officialStudent.getTel());
            entity.setLossClass(officialStudent.getClazz());
            entity.setMarketingPerson(officialStudent.getMarketingPerson());
            entity.setAuditState(0);
            //经办人
            Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
            entity.setResponsible(employee);
            //录入人
            entity.setEntryUser(employee);

            studentLossItemMapper.insert(entity);
            officialStudentMapper.updateStudyStateLoss(officialStudent.getId());
        }
    }

    public PageResult classOrder(ChartQueryObj a) {

        if (a.getTypeId() == 0){
            //班级
            //查总数
            int total = officialStudentMapper.queryForCountClassOrder();
            if(total == 0){
                return new PageResult();
            }
            List<ClassOrder> rows = officialStudentMapper.classOrder();
            return new PageResult(total, rows);

        }else{
            //支付方式
            //查总数
            int total = officialStudentMapper.queryForCountPaymentOrder();
            if(total == 0){
                return new PageResult();
            }
            List<ClassOrder> rows = officialStudentMapper.paymentOrder();
            return new PageResult(total, rows);
        }

    }

    public List<Map<String, Object>> selectSaleChart() {
        return officialStudentMapper.selectSaleChart();
    }

}
